class MapNavigator {
  constructor(destinationName, latitude, longitude,address = null) {
    this.destinationName = destinationName;
    this.latitude = latitude;
    this.longitude = longitude;
	this.address = address;
  }

  // 小程序平台的跳转：使用 wx.openLocation
  openLocationInMiniProgram() {
    wx.openLocation({
      latitude: this.latitude,
      longitude: this.longitude,
      name: this.destinationName,
      address: this.address || this.destinationName,
      appParam: 'src=weixin&refer=weixin'
    });
  }
  
  openH5App(){
	const url = `https://uri.amap.com/navigation?to=${this.longitude},${this.latitude},${encodeURIComponent(this.destinationName)}&mode=car&src=uniapp&callnative=1`;
	window.location.href = url;
  }

  // 跳转到高德地图App
  openGaodeApp(type) {
	  let url=''
    if(type===1){
		url = `iosamap://navi?sourceApplication=uniapp&lat=${this.latitude}&lon=${this.longitude}&destName=${encodeURIComponent(this.destinationName)}&dev=0&m=0&mode=2`;
	}else{
		url = `androidamap://navi?sourceApplication=uniapp&lat=${this.latitude}&lon=${this.longitude}&destName=${this.destinationName}&dev=0&m=0&mode=2`;
	}
	plus.runtime.openURL(url, (error) => {
	    console.error('打开高德地图失败:', error.message);
	});

  }



  // 判断平台并跳转
  navigateToMap() {
    const systemInfo = uni.getSystemInfoSync();
    const isApp = systemInfo.platform === 'ios' || systemInfo.platform === 'android'; // 判断是否是原生App
    const isH5 = systemInfo.platform === 'web'; // 判断是否是 H5
    const isMiniProgram = systemInfo.platform === 'mp'; // 判断是否是小程序平台

    if (isMiniProgram) {
     this.openLocationInMiniProgram();
    } else if(isH5){
		this.openH5App()
	} else if(isApp){
		let type = systemInfo.platform === 'ios' ? 1:2
     this.openGaodeApp(type);  
    } else {
      uni.showToast({
        title: '无法判断平台，无法跳转',
        icon: 'none'
      });
    }
  }
}

export default MapNavigator;
